################################################################################
# Judicial Ideology as Text -- Table A5: Comparing 2021 MQ vs. 2021 Dynamic Wordshoal
# Truscott & Romano
# Updated February 2025
################################################################################


################################################################################
# Load Source
################################################################################

source('R/truscott_romano_source.R') # Load Source

################################################################################
# Table A5: 2021 MQ v. 2021 Dynamic Wordshoal
################################################################################

{

  dynamic_all <- bind_rows(lapply(names(wordshoals_dynamic$High), function(year) {
    df <- wordshoals_dynamic$High[[year]]$summary_output
    df$year <- year
    return(df)
  })) %>%
    group_by(author) %>%
    summarise(mean_theta = mean(theta))

  static_all <- wordshoals_static$High$summary_output %>%
    dplyr::select(author, theta)

  mq_21 <- mq %>%
    mutate(justiceName = sub(".*([A-Z])", "\\1", justiceName)) %>%
    mutate(justiceName = ifelse(justiceName == 'Connor', "O'Connor", justiceName)) %>%
    filter(justiceName %in% dynamic_all$author) %>%
    group_by(justiceName) %>%
    summarise(post_mn = mean(post_mn)) %>%
    dplyr::select(justiceName, post_mn) %>%
    rename(author = justiceName,
           mq = post_mn)

  Table_A5 <- tibble(mq_21 %>%
                   left_join(dynamic_all, by = 'author') %>%
           left_join(static_all, by = 'author') %>%
                   arrange(mean_theta) %>%
             mutate(mq = round(mq, 3),
                    theta = round(theta, 3),
                    mean_theta = round(mean_theta,3)) %>%
                   rename(Author = author,
                          `Martin-Quinn` = mq,
                          `Static Wordshoal` = theta,
                          `Dynamic Wordshoal` = mean_theta))

  table_A5_text <- stargazer(Table_A5, type = 'text', rownames = F, summary = F, align = T)
  table_A5_latex <- stargazer(Table_A5, type = 'latex', rownames = F, summary = F, align = T, label = 'tab:table_A5', title = 'Comparison of Static and Dynamic Wordshoal Estimates Versus Martin-Quinn by Justice', style = 'apsr')

  writeLines(table_A5_text, 'tables/Table_A5_text.txt', sep = '\n')
  writeLines(table_A5_latex, 'tables/Table_A5_latex.txt', sep = '\n')

} # Table A5: 2021 MQ v. 2021 Dynamic Wordshoal



